<%@ tag import="com.eischet.lookup.web.WebApplication" %>
<%@ tag display-name="renders a widget that allows paging through a result set" %>
<%@ tag language="java" pageEncoding="utf-8" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="lookup" %>
<%@ attribute name="userQuery" required="true" type="java.lang.String" %>
<%@ attribute name="type" required="true" type="java.lang.String" %>
<%@ attribute name="total" required="true" type="java.lang.Integer" %>
<%@ attribute name="length" required="true" type="java.lang.Integer" %>
<%@ attribute name="startIndex" required="true" type="java.lang.Integer" %>
<%@ attribute name="pageSize" required="true" type="java.lang.Integer" %>
<%@ attribute name="fromDate" required="true" type="java.lang.String" %>
<%@ attribute name="toDate" required="true" type="java.lang.String" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="lookup" %>

<div class="pager">
    <% if (total <= length && pageSize == 10) { %>
        <div class="page">&nbsp;</div>
    <% } else { %>
    <%
        int previous = startIndex - pageSize;
        int next = startIndex + pageSize;
        int last = total - ( total % pageSize );
        if (last == total && last > pageSize) {
            last -= pageSize; // fixes a possible off-by-one error in finding the last page, discovered by Swisscom
        }

        boolean hasPrevious = startIndex > 0;
        boolean hasNext = total > startIndex + length;

        if (hasPrevious) {%>
            <span class='enabled'>
                <lookup:paginglink userQuery="<%=userQuery%>" type="<%=type%>" startIndex="0" pageSize="<%=pageSize%>" fromDate="<%=fromDate%>" toDate="<%=toDate%>">
                    <img src="gfx/first.gif">
                    First
                </lookup:paginglink>
            </span>
            <span>
                <lookup:paginglink userQuery="<%=userQuery%>" type="<%=type%>" startIndex="<%=previous%>" pageSize="<%=pageSize%>" fromDate="<%=fromDate%>" toDate="<%=toDate%>">
                    <img src="gfx/previous.gif">
                    Previous
                </lookup:paginglink>
            </span>
        <% } else { %>
            <span class="disabled">
                <img src="gfx/first.gif">
                First
            </span>
            <span class="disabled">
                <img src="gfx/previous.gif">
                Previous
            </span>
        <% } %>

        <form action='lookup.app' method='GET'>
            <input type='hidden' name='do' value='r'>
            <input type="hidden" name="fromdate" value="<%=fromDate%>">
            <input type="hidden" name="todate" value="<%=toDate%>">
            <input type="hidden" name="q" value="<%=userQuery%>">
            <input type="hidden" name="t" value="<%=type%>">
            <input type="hidden" name="s" value="<%=startIndex%>">

            show

            <select name="p" onchange="return this.form.submit();">
                <% for (Integer possiblePageSize: WebApplication.VALID_PAGE_SIZES) { %>
                    <% if (pageSize.equals(possiblePageSize)) { %>
                        <option value="<%=possiblePageSize%>" selected="selected"><%=possiblePageSize%></option>
                    <% } else { %>
                        <option value="<%=possiblePageSize%>"><%=possiblePageSize%></option>
                    <% } %>
                <% } %>
            </select>

            results per page

        </form>

        <% if (hasNext) { %>
            <span class='enabled'>
                <lookup:paginglink userQuery="<%=userQuery%>" type="<%=type%>" startIndex="<%=next%>" pageSize="<%=pageSize%>" fromDate="<%=fromDate%>" toDate="<%=toDate%>">
                    <img src="gfx/next.gif">
                    Next
                </lookup:paginglink>
            </span>
            <span class='enabled'>
                <lookup:paginglink userQuery="<%=userQuery%>" type="<%=type%>" startIndex="<%=last%>" pageSize="<%=pageSize%>" fromDate="<%=fromDate%>" toDate="<%=toDate%>">
                    <img src="gfx/last.gif">
                    Last
                </lookup:paginglink>
            </span>
        <% } else { %>
            <span class='disabled'>
                Next
                <img src="gfx/next.gif">
            </span>
            <span class="disabled">
                Last
                <img src="gfx/last.gif">
            </span>
        <% } %>
    <% } %>
</div>

